<template>
	<view>
		<view class="main">
			<u-form :model="form" ref="uForm" label-width="160">
				<u-form-item label="原密码" prop="oldPassword">
					<u-input v-model="form.oldPassword"></u-input>
				</u-form-item>
				<u-form-item label="新密码" prop="newPassword">
					<u-input v-model="form.newPassword"></u-input>
				</u-form-item>
				<u-form-item label="确认密码" prop="rePassword">
					<u-input v-model="form.rePassword"></u-input>
				</u-form-item>
			</u-form>
		</view>
		<button @click="submit">修改密码</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					oldPassword: '',
					newPassword: '',
					rePassword: ''
				},
				rules: {
					oldPassword: [
						{
							required: true,
							message: '原密码不能为空',
							trigger: ['blur','change']
						}
					],
					newPassword: [
						{
							required: true,
							message: '新密码不能为空',
							trigger: ['blur','change']
						}
					],
					rePassword: [
						{
							validator: (rule, value, callback) => {
								if(value !== this.form.newPassword){
									callback(new Error('两次密码输入不一致'))
								}else{
									callback()
								}
							},
							trigger: ['blur','change']
						}
					]
				}
			};
		},
		onLoad() {
			
		},
		methods: {
			submit(){
				this.$refs.uForm.validate(valid => {
					if(valid){
						this.$request({
							url: '/prod-api/api/common/user/resetPwd',
							method: 'PUT',
							data: {
								...this.form
							}
						}).then(res => {
							if(res.code !== 200){
								uni.showToast({
									icon:'error',
									title:res.msg
								})
							}else{
								uni.showToast({
									title:'修改密码成功'
								})
								setTimeout(() => {
									uni.switchTab({
										url:'/pages/user/user'
									})
								}, 1000)
							}
						})
					}
				})
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #f7f7f7;
		padding: 20rpx;
		.main{
			background-color: #fff;
			padding: 20rpx;
			border-radius: 20rpx;
			u-form{
				padding: 20rpx;
			}
		}
		button{
			margin-top: 40rpx;
			background-color: #2979ff;
			color: white;
			width: 200rpx;
			height: 80rpx;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
</style>
